<?php
namespace app\common\loader;

class Mock {
    private $module = '';
    private $anyInstances = [];

    public function __construct($module) {
        $this->module = $module;
    }

    public function get_base() {
        return 'app\\' . $this->module;
    }

    //把模块名传给event
    public function filter_event_args($args) {
        array_unshift($args, $this->module);
        return $args;
    }

    //地址生成any
    public function classAny($any, $name) {
        if(strpos($name, 'app\\') === false) {
            $name = $this->get_base() . '\\' . $any . '\\' . $name;
        }
        return $name;
    }

    //生成any类地址
    public function loadAny($any, $name = null, $args = []) {
        if($name === null) {
            $name = 'Index';
        }
        if(isset($this->instances[$any])) {
            if(isset($this->instances[$any][$name])) {
                return $this->instances[$any][$name];
            }
        } else {
            $this->instances[$any] = [];
        }
        $classname = $this->classAny($any, $name);
        if (!class_exists($classname)) {
            if($this->module != 'common') {
                return loader()->loadAny($any, $name);
            } else {
                return null;
            }
        }
        $filterFun = 'filter_' . $name . '_args';
        if(method_exists($this, $filterFun)) {
            $args = $this->$filterFun($args);
        }
        $obj = new $classname(...$args);
        $this->instances[$any][$name] = $obj;
        return $obj;
    }

    //生成模型类地址
    public function classModel($name) {
        if(strpos($name, 'app\\') === false) {
            $name = $this->get_base() . '\\model\\' . $name;
        }
        return $name;
    }

    //加载模型
    public function loadModel($name) {
        return $this->loadAny('model', $name);
    }

    //生成验证器类地址
    public function classValidate($name) {
        if(strpos($name, 'app\\') === false) {
            $name = $this->get_base() . '\\validate\\' . $name;
        }
        return $name;
    }

    //加载验证器
    public function loadValidate($name, $args = []) {
        return $this->loadAny('validate', $name, $args);
    }

    //生成工具类地址
    public function classTool($name) {
        if(strpos($name, 'app\\') === false) {
            $name = $this->get_base() . '\\tool\\' . $name;
        }
        return $name;
    }

    //加载工具类
    public function loadTool($name, $args = []) {
        return $this->loadAny('tool', $name, $args);
    }

    //生成流程地址
    public function classProcess($name) {
        if(strpos($name, 'app\\') === false) {
            $name = $this->get_base() . '\\process\\' . $name;
        }
        return $name;
    }

    //加载流程类
    public function loadProcess($name, $args = []) {
        return $this->loadAny('process', $name, $args);
    }

    //生成枚举地址
    public function classEnum($name) {
        if(strpos($name, 'app\\') === false) {
            $name = $this->get_base() . '\\enum\\' . $name;
        }
        return $name;
    }

    //加载枚举类
    public function loadEnum($name, $args = []) {
        return $this->loadAny('enum', $name, $args);
    }

    //生成单执地址
    public function classOnly($name) {
        if(strpos($name, 'app\\') === false) {
            $name = $this->get_base() . '\\only\\' . $name;
        }
        return $name;
    }

    //加载单执类
    public function loadOnly($name, $args = []) {
        return $this->loadAny('only', $name, $args);
    }
}
